package com.mdd.front.service.impl.zdjd;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.zdjd.TZdjdRechargeRecord;
import com.mdd.common.mapper.zdjd.TZdjdRechargeRecordMapper;
import com.mdd.common.util.TimeUtils;
import com.mdd.front.service.zdjd.ITZdjdRechargeRecordService;
import com.mdd.front.validate.common.PageValidate;
import com.mdd.front.validate.zdjd.TZdjdRechargeRecordCreateValidate;
import com.mdd.front.validate.zdjd.TZdjdRechargeRecordSearchValidate;
import com.mdd.front.validate.zdjd.TZdjdRechargeRecordUpdateValidate;
import com.mdd.front.vo.zdjd.TZdjdRechargeRecordDetailVo;
import com.mdd.front.vo.zdjd.TZdjdRechargeRecordListedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 设备充值记录实现类
 * @author LikeAdmin
 */
@Service
public class TZdjdRechargeRecordServiceImpl implements ITZdjdRechargeRecordService {
        
    @Resource
    TZdjdRechargeRecordMapper tZdjdRechargeRecordMapper;

    /**
     * 设备充值记录列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<TZdjdRechargeRecordListedVo>
     */
    @Override
    public PageResult<TZdjdRechargeRecordListedVo> list(PageValidate pageValidate, TZdjdRechargeRecordSearchValidate searchValidate) {
        Integer page  = pageValidate.getPage_no();
        Integer limit = pageValidate.getPage_size();

        QueryWrapper<TZdjdRechargeRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        tZdjdRechargeRecordMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:deviceId@device_id:int",
            "=:deviceCode@device_code:str",
            "=:agentId@agent_id:int",
            "like:agentName@agent_name:str",
            "=:userId@user_id:int",
            "=:rechargeAmount@recharge_amount:long",
            "=:year:str",
            "=:month:str",
            "=:day:str",
        });

        IPage<TZdjdRechargeRecord> iPage = tZdjdRechargeRecordMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<TZdjdRechargeRecordListedVo> list = new LinkedList<>();
        for(TZdjdRechargeRecord item : iPage.getRecords()) {
            TZdjdRechargeRecordListedVo vo = new TZdjdRechargeRecordListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.timestampToDate(item.getCreateTime().getTime()));
            vo.setUpdateTime(TimeUtils.timestampToDate(item.getUpdateTime().getTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 设备充值记录详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return TZdjdRechargeRecord
     */
    @Override
    public TZdjdRechargeRecordDetailVo detail(Integer id) {
        TZdjdRechargeRecord model = tZdjdRechargeRecordMapper.selectOne(
                new QueryWrapper<TZdjdRechargeRecord>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        TZdjdRechargeRecordDetailVo vo = new TZdjdRechargeRecordDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 设备充值记录新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(TZdjdRechargeRecordCreateValidate createValidate) {
        TZdjdRechargeRecord model = new TZdjdRechargeRecord();
        model.setDeviceId(createValidate.getDeviceId());
        model.setDeviceCode(createValidate.getDeviceCode());
        model.setAgentId(createValidate.getAgentId());
        model.setAgentName(createValidate.getAgentName());
        model.setUserId(createValidate.getUserId());
        model.setRechargeAmount(createValidate.getRechargeAmount());
        model.setCreateTime(new Date());
        model.setUpdateTime(new Date());
        model.setYear(createValidate.getYear());
        model.setMonth(createValidate.getMonth());
        model.setDay(createValidate.getDay());
        tZdjdRechargeRecordMapper.insert(model);
    }

    /**
     * 设备充值记录编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(TZdjdRechargeRecordUpdateValidate updateValidate) {
        TZdjdRechargeRecord model = tZdjdRechargeRecordMapper.selectOne(
                new QueryWrapper<TZdjdRechargeRecord>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setDeviceId(updateValidate.getDeviceId());
        model.setDeviceCode(updateValidate.getDeviceCode());
        model.setAgentId(updateValidate.getAgentId());
        model.setAgentName(updateValidate.getAgentName());
        model.setUserId(updateValidate.getUserId());
        model.setRechargeAmount(updateValidate.getRechargeAmount());
        model.setUpdateTime(new Date());
        model.setYear(updateValidate.getYear());
        model.setMonth(updateValidate.getMonth());
        model.setDay(updateValidate.getDay());
        tZdjdRechargeRecordMapper.updateById(model);
    }

    /**
     * 设备充值记录删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        TZdjdRechargeRecord model = tZdjdRechargeRecordMapper.selectOne(
                new QueryWrapper<TZdjdRechargeRecord>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        tZdjdRechargeRecordMapper.delete(new QueryWrapper<TZdjdRechargeRecord>().eq("id", id));
    }

    @Override
    public List<TZdjdRechargeRecord> selectListByYearMonthDay(int year, int month, int day) {
        QueryWrapper<TZdjdRechargeRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("year", year).eq("month", month).eq("day", day);
        List<TZdjdRechargeRecord> selectList = tZdjdRechargeRecordMapper.selectList(wrapper);
        return Optional.ofNullable(selectList).orElse(new ArrayList<>());
    }

}
